<?php
namespace app\admin\model\template;

use think\facade\Log;
use think\facade\Request;

class CheckFail
{

    protected $data;

    protected $template_id;

    protected $template_remark;

    /**
     * 架构函数
     *
     * @access public
     * @param array $data
     *            数据
     */
    public function __construct(array $data = [])
    {
        $this->data = $data;
        
        $this->template_id = sysconfig("template", "check_fail_template");
        $this->template_remark = sysconfig("template", "check_fail_remark");
    }

    /**
     * 获取模板发送数据
     */
    private function getData()
    {
        $params['touser'] = $this->data['openid'];
        $params['template_id'] = $this->template_id;
        if (! empty($this->data['url'])) {
            $params['page'] = $this->data['url'];
        } else {
            $params['page'] = "/pages/index/index";
        }
        $params['data'] = array(
            'thing6' => array(
                'value' => $this->data['type'],
                'color' => '#ff510'
            ),
            'thing1' => array(
                'value' => $this->data['name'],
                'color' => '#ff510'
            ),
            'phrase3' => array(
                'value' => $this->data['result'],
                'color' => '#ff510'
            ),
            'thing4' => array(
                'value' => $this->data['refuse'],
                'color' => '#ff510'
            ),
            'thing5' => array(
                'value' => $this->template_remark,
                'color' => '#ff510'
            )
        );
        
        $params = json_encode($params, true);
        return $params;
    }

    public function send()
    {
        $token = getAccessToken();
        if (empty($token)) {
            Log::error("CheckFail template 获取用户token失败", $this->data);
            return;
        }
        $sendParams = $this->getData();
        $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $token;
        $re = wtw_request($url, $sendParams);
        
        Log::write("CheckFail template send" . json_encode(array(
            'url' => $url,
            'sendParams' => $sendParams,
            're' => $re
        )), 'info');
        
        return $re;
    }
}